import { _decorator, Component, Node, AudioSource, AudioClip } from 'cc'
const { ccclass, property, menu } = _decorator

/**
 * Predefined variables
 * Name = AudioManager
 * DateTime = Tue Feb 22 2022 18:42:58 GMT+0800 (中国标准时间)
 * Author = ashscc
 * FileBasename = AudioManager.ts
 * FileBasenameNoExtension = AudioManager
 * URL = db://assets/script/framework/AudioManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/en/
 *
 */

interface IAudioClips {
  [name: string]: AudioClip
}

@ccclass('AudioManager')
@menu('framework/AudioManager')
export class AudioManager extends Component {
  @property([AudioClip])
  public audioClips: AudioClip[] = []

  private _iaudioClips: IAudioClips = {}
  private _audioSource: AudioSource = null

  start() {
    for (let index = 0; index < this.audioClips.length; index++) {
      const element = this.audioClips[index]
      this._iaudioClips[element.name] = element
    }

    this._audioSource = this.getComponent(AudioSource)
  }

  public play(name: string){
    const audioClip = this._iaudioClips[name]
    if(audioClip !== undefined){
        this._audioSource.playOneShot(this._iaudioClips[name])
    }
  }

  // update (deltaTime: number) {
  //     // [4]
  // }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/en/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/en/scripting/life-cycle-callbacks.html
 */
